package com.meu.prontuario.database;

import java.util.ArrayList;
import java.util.List;

import com.meu.prontuario.util.Adulto;
import com.meu.prontuario.util.CommandSQL;
import com.meu.prontuario.util.Year;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class ADULTO extends SQLiteOpenHelper{
	
	private static final String TABLE = "ADULTO" +Year.getInstance().getYear()+ ".db";
	private static final int VERSION  = 1;


	public ADULTO (Context context){
		super(context,TABLE,null,VERSION);
	}
	
	public ADULTO(Context context, String name, CursorFactory factory,	int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//db.execSQL("CREATE TABLE " + TABLE + " ( " + CommandSQL.ADULTO + ");");
		String ddl = "CREATE TABLE "+ TABLE +
				 "(id INTEGER PRIMARY KEY," +
				 "name TEXT UNIQUE NOT NULL," +
				 "idade TEXT NOT NULL," +
				 "sexo TEXT NOT NULL," +
				 "cidade TEXT NOT NULL);";
		db.execSQL(ddl);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	
	}
	
	public void insert(Adulto adulto){
		
	}
	
	public List<Adulto> getList(){
		List<Adulto> adultos = new ArrayList<Adulto>();
		Cursor cursor = getReadableDatabase().query(TABLE, null, null, null, null, null, null);
		
		while (cursor.moveToNext()){
			
		}
		cursor.close();
			
		return adultos;		
	}
	
	public Adulto getItemPorId(Long id){
		Adulto adulto = new Adulto();
		
		Cursor cursor = getWritableDatabase().query(TABLE, null,"id = ?",new String []{}/*adulto.getId().toString()}*/,null,null, null);
		cursor.moveToFirst();
		
		return adulto;
	}
	
	public void change(Adulto adulto){
		ContentValues values = new ContentValues();
			
		getWritableDatabase().update(TABLE, values,"id = ?",new String [] {});
	}
	
	public void delete(Adulto adulto){
		getWritableDatabase().delete(TABLE, "id = ?",new String []{});
	}


}
